package com.pocket.mic.common.util;


import java.util.ArrayList;
import java.util.Date;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.pocket.mic.model.Card;
import com.pocket.mic.model.Game;

public class JSONUtil {
	
	public static String getJSON(Object bean) throws Exception {
//		JsonConfig jsonConfig = getDateJsonConfig(null, TimeUtil.FORMAT_YMDHMS);
//		JSON json = JSONSerializer.toJSON(bean, jsonConfig);
		JSON json = JSONSerializer.toJSON(bean);
		return json.toString();
	}
	

	public static String getJSON(Object bean, String[] excludes, String datePattern) throws Exception {
		JsonConfig jsonConfig = getDateJsonConfig(excludes, datePattern);
		JSON json = JSONSerializer.toJSON(bean, jsonConfig);
		return json.toString();
	}
	
	public static String getJSON(Object bean, String datePattern) throws Exception {
		return getJSON(bean, null, datePattern);
	}
	
	private static JsonConfig getDateJsonConfig(String[] excludes, String datePattern) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		jsonConfig.setIgnoreDefaultExcludes(false);
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDataValueProcessor(datePattern));
		jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonDataValueProcessor(TimeUtil.FORMAT_YMDHMS));
		
		return jsonConfig;
	}

	public static void main(String[] args) {
		Game game=new Game();
//		game.setId(11011l);
		game.setMainPool(90000);
		game.setCards(new ArrayList<Card>(5));
		game.setStartTime(new Date(System.currentTimeMillis()));
		try {
//			System.out.println(JSONUtil.getJSON(game));
			System.out.println(JSONSerializer.toJSON(game));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
